/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.data.collections.Map;
import com.bourre.data.iterator.Iterator;

import fever.log.FeverDebug;

import flash.display.BitmapData;

/**
 * {@link fever.data.libs.BitmapLib} repository.
 * 
 * @see fever.data.libs.BitmapLib
 * 
 * @author Romain Ecarnot
 */
class fever.data.libs.BitmapLibLocator
{
	//-------------------------------------------------------------------------
	// Constants definition
	//-------------------------------------------------------------------------
	
	/** Default bitmapData repository name. */
	public static function get DEFAULT() : String  { return '__DBR__'; }
	
	
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _m : Map = new Map();
	
	private var _name : String;
	private var _cache : Map;

	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns {@link BitmapLibLocator} registred with passed-in {@code name}.
	 * 
	 * <p>If {@code name} not exist, a new {@link BitmapLibLocator} is created with 
	 * passed-in {@code name} for registration.
	 * 
	 * <p>If {@code name} is {@code null} or {@code undefined}, returns the 
	 * default {@link BitmapLibLocator} named {@link #DEFAULT}
	 * 
	 * @return {@link BitmapLibLocator} instance
	 */
	public static function getLocator( name : String ) : BitmapLibLocator
	{
		if( !name ) name = BitmapLibLocator.DEFAULT;
		
		if( !_m.containsKey( name ) )
		{
			_m.put( name, new BitmapLibLocator( name ) );
		}
		return _m.get( name );
	}
	
	/**
	 * Returns {@code true} if a {@link BitmapLibLocator} is registred with 
	 * the passed-in {@code name} id, otherwise {@code false}
	 */
	public static function isRegistredLocator( name : String ) : Boolean
	{
		return _m.containsKey( name );
	}
	
	/**
	 * Unregistrers {@link BitmapLibLocator} defined by passed-in {@code name} id.
	 * 
	 * <p>All data stored in this {@link BitmapLibLocator} are lost.
	 */
	public static function unregisterLocator( name : String ) : Boolean
	{
		if( _m.containsKey( name ) )
		{
			var o : BitmapLibLocator = _m.get( name ); 
			o.disposeBitmap();
			_m.remove( name );
			return true;	
		}
		return false;
	}
	
	/**
	 * Retreives {@code BitmapData} object from passed-in full 
	 * qualified {@code id}.
	 * 
	 * <p>id must be :ID@LOCATOR_NAME
	 * 
	 * <p>Useful when define icons in xml config file for example.
	 */
	public static function getFullQualifiedBitmap( id : String ) : BitmapData
	{
		var pattern : Array = id.split( "@" );
		var iconID : String = pattern[0];
		var stock : String	= pattern[1];
		
		if( !stock ) stock = BitmapLibLocator.DEFAULT;
		if( BitmapLibLocator.isRegistredLocator( stock ) )
		{
			var r : BitmapLibLocator = BitmapLibLocator.getLocator( stock );
			if( r.isRegistredBitmap( iconID ) ) return r.getBitmap( iconID );
		} 
		return null;
	}
	
	/**
	 * Returns {@code true} if passed-in full qualified bitmap {@code id} exist.
	 * 
	 * <p>id must be :BITMAP_ID@REPOSITORY_NAME
	 */
	public static function isFullQualifiedBitmapExist( id : String ) : Boolean
	{
		var pattern : Array = id.split( "@" );
		var iconID : String = pattern[0];
		var stock : String	= pattern[1];
		
		if( !stock ) stock = BitmapLibLocator.DEFAULT;
		if( BitmapLibLocator.isRegistredLocator( stock ) )
		{
			return BitmapLibLocator.getLocator( stock ).isRegistredBitmap( iconID );
		}
		return false;	
	}
	
	/**
	 * Stores a new {@code BitmapData} object with {@code id} name.
	 */
	public function pushBitmap( id : String, bmp : BitmapData ) : Boolean
	{
		if( !_cache.containsKey( id ) )
		{
			_cache.put( id, bmp );
			return true;
		} 
		else
		{
			FeverDebug.ERROR( 'BitmapData with name ' + id +  ' is already registred' );
			return false;
		}
	}
	
	/**
	 * Returns {@code BitmapData} stored with passed-in {@code id}
	 */
	public function getBitmap( id : String ) : BitmapData
	{
		if( !_cache.containsKey( id ) )
		{
			FeverDebug.ERROR( 'BitmapData ' + id + ' is not registred in ' + _name );		
			return null;
		}	
		return BitmapData( _cache.get( id ) ).clone();
	}
	
	/**
	 * Returns {@code true} if a {@code BitmapData} is registred with 
	 * the passed-in {@code id}, otherwise {@code false}
	 */
	public function isRegistredBitmap( id : String ) : Boolean
	{
		return _cache.containsKey( id );	
	}
	
	/**
	 * Removes {@code BitmapData} stored with passed-in {@code id}
	 */
	public function removeBitmap( id : String ) : Boolean
	{
		if( _cache.containsKey( id ) )
		{
			var bmp : BitmapData = _cache.get( id );
			bmp.dispose();
			
			_cache.remove( id );	
			return true;
		}
		return false;
	}
	
	/**
	 * Clears all {@code BitmapData} object stored in current 
	 * {@link BitmapLibLocator}
	 */
	public function disposeBitmap() : Void
	{
		var it : Iterator = _cache.getValuesIterator();
		
		while( it.hasNext() )
		{
			var bmp : BitmapData = it.next();
			bmp.dispose();	
		}	
		_cache.clear();
	}
	
	/**
	 * Returns locator name.
	 */
	public function getLocatorName() : String
	{
		return _name;	
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function BitmapLibLocator( name : String )
	{
		_cache = new Map();
		_name = name;
	}		
}